﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

using H2.NET.Controls;
using H2.NET.Business;
using H2.NET.Data.Common;

using System.IO;
using H2.NET.Data.Exceller;



namespace H2.NET.Web.Pages
{
    public partial class PHBP : TaskPage
    {
        #region 属性 Public

        private bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

      
        #endregion

       
        #region 事件 Page_Load(页面加载)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                INFO.Text = string.Format("<br/>表单正在被同事“{0}”抢占处理，拒绝提交！", RobUser);
                INFO.Visible = IsOverFlow;

                //InitDropDownList();
                
                #region 发起步骤
                if (TaskAPI.IsInitiate)
                {
                   // VD.Checked = true;
 
                }
                #endregion

                #region 审批环节
                else
                {
                    #region 加载数据并显示
                    hk_BasePrice listp = (hk_BasePrice.process == TaskAPI.Process & hk_BasePrice.incident == TaskAPI.Incident).ListFirst<hk_BasePrice>();
                    if (listp != null)
                    {
                        ApplicationNo.Text = listp.ApplicationNo;
                        //VD.Checked                          = vendor.VD_Checked == "1";
                        //SA.Checked = vendor.SA_Checked == "1";
                        VD_Category.SelectedValue = listp.VD_Category;
                        Condition_Type.Text = listp.Condition_Type;
                        Sorg_DC.Checked = listp.Sorg_DC == "1";
                        Sales_Organization.Text = listp.Sales_Organization;
                        Distribution_Channel.Text = listp.Distribution_Channel;
                        Division.SelectedValue = listp.Division;
                        DrpPriority.SelectedValue = listp.DrpPriority;

                        DMO_DrpPriority.SelectedValue = listp.DMO_DrpPriority;
                        DMO_Arrpvoe_Creation_Number.Text = listp.DMO_Arrpvoe_Creation_Number;
                        DMO_Arrpvoe_Creation_Time.Text = listp.DMO_Arrpvoe_Creation_Time;
                        DMO_Maintain_Creation_Number.Text = listp.DMO_Maintain_Creation_Number;
                        DMO_Maintain_Creation_Time.Text = listp.DMO_Maintain_Creation_Time;
                        DMO_Check_Creation_Number.Text = listp.DMO_Check_Creation_Number;
                        DMO_Check_Creation_Time.Text = listp.DMO_Check_Creation_Time;
                        DMO_Arrpvoe_Revision_Number.Text = listp.DMO_Arrpvoe_Revision_Number;
                        DMO_Arrpvoe_Revision_Time.Text = listp.DMO_Arrpvoe_Revision_Time;
                        DMO_Maintain_Revision_Number.Text = listp.DMO_Maintain_Revision_Number;
                        DMO_Maintain_Revision_Time.Text = listp.DMO_Maintain_Revision_Time;
                        DMO_Check_Revision_Number.Text = listp.DMO_Check_Revision_Number;
                        DMO_Check_Revision_Time.Text = listp.DMO_Check_Revision_Time;
                    }
                    #endregion

                    #region 审批项初始化
                    switch (TaskAPI.Step)
                    {
                        case "DMO Approve":
                        case "DMO Maintain":
                        case "DMO Check":
                            Assessment1.Option.Add(new t_AssessmentOption("Finish", "1", true));
                            Assessment1.Option.Add(new t_AssessmentOption("Return", "2", false));
                            break;

                    }
                    #endregion
                }
                #endregion

                #region 设置控件禁启用
                bool flg = (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit) ? true : false;

                SetEnable(flg, VD_Category, Division,DrpPriority);
                //SetEnable(TaskAPI.Step == "DMO Maintain", SAP_Code);
                SetEnable(TaskAPI.Step == "DMO Approve" | TaskAPI.Step == "DMO Maintain" | TaskAPI.Step == "DMO Check", DMO_DrpPriority, DMO_Arrpvoe_Creation_Number, DMO_Arrpvoe_Creation_Time, DMO_Maintain_Creation_Number, DMO_Maintain_Creation_Time, DMO_Check_Creation_Number, DMO_Check_Creation_Time, DMO_Arrpvoe_Revision_Number, DMO_Arrpvoe_Revision_Time, DMO_Maintain_Revision_Number, DMO_Maintain_Revision_Time, DMO_Check_Revision_Number, DMO_Check_Revision_Time);
                SetVisible(TaskAPI.Step == "DMO Approve" | TaskAPI.Step == "DMO Maintain" | TaskAPI.Step == "DMO Check", P_DMO);
                SetVisible(flg, Base_Price_Add, Base_Price_Delete);
                Assessment1.Enable = TaskAPI.IsAllowSubmit;
                SetVisible(TaskAPI.IsAllowSubmit, Send, Split);

                //if (TaskAPI.Step == "DMO Approve")
                //{
                //    SetForeColor(Color.Blue, DMO_DrpPriority_L);
                //}

                //if (TaskAPI.Step == "DMO Approve" & VD_Category.SelectedValue == "1")
                //{
                //    SetForeColor(Color.Blue, DMO_Arrpvoe_Creation_L);
                //}
                //if (TaskAPI.Step == "DMO Maintain" & VD_Category.SelectedValue == "1")
                //{
                //    SetForeColor(Color.Blue, DMO_Maintain_Creation_L);
                //}
                //if (TaskAPI.Step == "DMO Check" & VD_Category.SelectedValue == "1")
                //{
                //    SetForeColor(Color.Blue, DMO_Check_Creation_L);
                //}
                //if (TaskAPI.Step == "DMO Approve" & VD_Category.SelectedValue != "1")
                //{
                //    SetForeColor(Color.Blue, DMO_Arrpvoe_Revision_L);
                //}
                //if (TaskAPI.Step == "DMO Maintain" & VD_Category.SelectedValue != "1")
                //{
                //    SetForeColor(Color.Blue, DMO_Maintain_Revision_L);
                //}
                //if (TaskAPI.Step == "DMO Check" & VD_Category.SelectedValue != "1")
                //{
                //    SetForeColor(Color.Blue, DMO_Check_Revision_L);
                //}

                Attachment1.AllowDelete = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
                Attachment1.AllowUpload = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
                //InitTabs();
                #endregion

                #region 加深颜色
                string item = null;
                string value = null;
                foreach (Control cr in this.Controls)
                {
                    if (cr is DropDownList)
                    {
                        DropDownList tb = (DropDownList)cr;
                        if (!tb.Enabled)
                        {
                            tb.Enabled = true;
                            item = tb.SelectedItem.Text;
                            value = tb.SelectedValue.ToString();
                            tb.Items.Clear();
                            tb.Items.Add(new ListItem(item, value));
                        }
                        tb.ForeColor = System.Drawing.Color.DimGray;
                    }
                }
                if (!flg || !TaskAPI.IsAllowSubmit)
                {

                    foreach (Control cr in form.Controls)
                    {
                        if (cr is DropDownList)
                        {
                            DropDownList tb = (DropDownList)cr;
                            if (!tb.Enabled)
                            {
                                tb.Enabled = true;
                                item = tb.SelectedItem.Text;
                                value = tb.SelectedValue.ToString();
                                tb.Items.Clear();
                                tb.Items.Add(new ListItem(item, value));
                            }
                            tb.ForeColor = System.Drawing.Color.DimGray;
                        }
                    }
                    foreach (Control cr in form.Controls)
                    {
                        if (cr is TextBox)
                        {
                            TextBox tb = (TextBox)cr;
                            if (!tb.Enabled)
                            {
                                tb.Enabled = true;
                                tb.ReadOnly = true;
                                tb.ForeColor = System.Drawing.Color.DimGray;
                            }
                        }
                    }

                }

                #endregion

            }
        }


        protected override void OnLoad(EventArgs e)
        {
            InitControls();
            base.OnLoad(e);
        }

        public void InitControls()
        {
            bool flg = TaskAPI.IsAllowSubmit && TaskAPI.IsBeginStep;
            gv_Base_Price.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.Label, 0, Align.Left, false, "No", InputType.None, null));
            gv_Base_Price.DynamicColumn.Add(new BoundColumn2("01", "Material", BoundColumnType2.TextBox, 60, Align.Left, flg, "Material", InputType.None, null));
            gv_Base_Price.DynamicColumn.Add(new BoundColumn2("02", "Description", BoundColumnType2.TextBox, 100, Align.Left, flg, "Description", InputType.None, null));
            gv_Base_Price.DynamicColumn.Add(new BoundColumn2("03", "Amount", BoundColumnType2.TextBox, 60, Align.Left, flg, "Amount", InputType.None, null));
            gv_Base_Price.DynamicColumn.Add(new BoundColumn2("04", "Unit", BoundColumnType2.TextBox, 60, Align.Left, flg, "Unit", InputType.None, null));
            gv_Base_Price.DynamicColumn.Add(new BoundColumn2("05", "Per", BoundColumnType2.TextBox, 60, Align.Left, false, "Per", InputType.None, null));
            gv_Base_Price.DynamicColumn.Add(new BoundColumn2("06", "UoM", BoundColumnType2.TextBox, 60, Align.Left, false, "Uom", InputType.None, null));
            gv_Base_Price.DynamicColumn.Add(new BoundColumn2("07", "Calculat.type", BoundColumnType2.TextBox, 60, Align.Left, flg, "Calculat_Type", InputType.None, null));
            gv_Base_Price.DynamicColumn.Add(new BoundColumn2("08", "ScaleBasis", BoundColumnType2.TextBox, 60, Align.Left, flg, "ScaleBasis", InputType.None, null));
            gv_Base_Price.DynamicColumn.Add(new BoundColumn2("09", "Valid From", BoundColumnType2.TextBox, 60, Align.Left, flg, "Valid_From", InputType.None, null));
            gv_Base_Price.DynamicColumn.Add(new BoundColumn2("10", "Valid To", BoundColumnType2.TextBox, 60, Align.Left, flg, "Valid_To", InputType.None, null));



            
            if (!Page.IsPostBack)
            {
                gv_Base_Price.Key = Key;
              

                //BOM_P1.Visible = true;

                #region 发起步骤
                if (TaskAPI.IsInitiate)
                {
                    List<hk_BasePrice_Gridview> BPM_s = new List<hk_BasePrice_Gridview>();
                    for (int i = 0; i < 5; i++)
                    {
                        BPM_s.Add(new hk_BasePrice_Gridview());
                        BPM_s[i].No = i + 1;
                    }
                    gv_Base_Price.DataSource = BPM_s;

                   
                }
                #endregion

                #region 审批环节
                else
                {
                    IList<hk_BasePrice_Gridview> BPM_s = (hk_BasePrice_Gridview.process == TaskAPI.Process & hk_BasePrice_Gridview.incident == TaskAPI.Incident).List<hk_BasePrice_Gridview>();
                    if (BPM_s.Count == 0)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            BPM_s.Add(new hk_BasePrice_Gridview());
                            BPM_s[i].No = i + 1;
                        }
                    }
                    gv_Base_Price.DataSource = BPM_s;

                  
                }
                #endregion
            }
            //SetVisible(flg, SEFI_1_Add, SEFI_2_Add);
        }
        #endregion

        
        
        #region 事件 Send
        protected void Send_Click(object sender, EventArgs e)
        {
            try
            {
                #region 数据验证
                if ((TaskAPI.IsBeginStep || TaskAPI.Step == "DMO Maintain" || TaskAPI.Step == "DMO Check" || TaskAPI.Step == "DMO Approve") && TaskAPI.IsAllowSubmit)// 如果是退回的情况，不存在验证
                {
                    List<string> vald_s = new List<string>();
                    vald_s.AddRange(DataValidation());
                    if (vald_s.Count > 0)
                    {
                        Anthem.Manager.AddScriptForClientSideEval(string.Format("ShowMessage(\"{0}\", {1});", CombineMessage(vald_s), vald_s.Count));
                        return;
                    }
                }
                #endregion

                #region 引擎传值
                if (TaskAPI.IsBeginStep)
                {
                    if (TaskAPI.IsInitiate)
                    {
                        ApplicationNo.Text        = Util.GenerateSequence(((object[])TaskAPI["PrefixNo"])[0].ToString() + "-" + System.DateTime.Now.ToString("yyMM"), "", 4);
                        TaskAPI.UserName          = UserAPI.UserName;
                        TaskAPI["ApplicationNo"]  = ApplicationNo.Text;
                        TaskAPI["ApplicatorName"] = UserAPI.UserFullName;
                        TaskAPI["Applicator"]     = UserAPI.UserName;
                        TaskAPI["Time"]           = System.DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss");
                        TaskAPI["ApplicatorEmail"] = UserAPI.Mail;
                        TaskAPI["Department"]     = UserAPI.Department;
                        TaskAPI["ServerName"] = Util.AppSettings("ServerName");
                    }
                    
                    //TaskAPI.Summary = (VD_Category.SelectedItem.Text.Split(',').Length >= 2 ? VD_Category.SelectedItem.Text.Split(',')[1].Trim() : VD_Category.Text)
                    //                + " / " + ApplicationNo.Text + (VD.Checked ? " / Vendor " : "") + (SA.Checked ? " / Employee " : "");
                    
                    TaskAPI.Summary = (VD_Category.SelectedItem.Text.Split(',').Length >= 2 ? VD_Category.SelectedItem.Text.Split(',')[1].Trim() : VD_Category.Text) + " / " + ApplicationNo.Text;



                    TaskAPI["Actions"]        = VD_Category.SelectedValue;
                    TaskAPI["Remark_by_Initiator"] = Assessment1.Assessment.Remark;
                    TaskAPI["DrpPriority"] = DrpPriority.SelectedItem.Text;


                    #region 从数据库取得流程步骤处理人
                    SortedList<string, string> rec_a = new SortedList<string, string>();

                    IList<mdc_Recipient> rec_s = (mdc_Recipient.process == TaskAPI.Process & (mdc_Recipient.distribute == VD_Category.SelectedValue | mdc_Recipient.distribute == "*") & mdc_Recipient.status == 1).List<mdc_Recipient>();
                    foreach (mdc_Recipient rec in rec_s)
                    {
                        if (!rec_a.ContainsKey(rec.Step))
                        {
                            rec_a.Add(rec.Step, rec.Recipient);
                            continue;
                        }

                        if (rec_a.Values[rec_a.IndexOfKey(rec.Step)].Contains(rec.Recipient))
                        {
                            continue;
                        }

                        rec_a[rec.Step] = rec_a.Values[rec_a.IndexOfKey(rec.Step)] + "," + rec.Recipient;
                    }

                    //步骤处理人赋值
                    for (int i = 0; i < rec_a.Keys.Count; i++)
                    {
                        TaskAPI["R_" + rec_a.Keys[i].Replace(" ", "_")] = rec_a.Values[i].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    }

                    #endregion
                }
                else
                {
                    TaskAPI["ApproveResult"] = Assessment1.Assessment.Opinion;
                    TaskAPI["TaskID"]        = TaskAPI.TaskID;
                }

                #endregion

                #region 提交步骤
                bool flg=false;
                Storage.TransactionExecute(o =>
                {
                    #region “Fill in the form”步骤
                    if (TaskAPI.IsBeginStep)
                    {
                        hk_BasePrice listp = new hk_BasePrice();
                        if ((hk_BasePrice.process == TaskAPI.Process & hk_BasePrice.incident == TaskAPI.Incident & hk_BasePrice.userName == UserAPI.UserName).Count<hk_BasePrice>() > 0)
                        {
                            listp = (hk_BasePrice.process == TaskAPI.Process & hk_BasePrice.incident == TaskAPI.Incident & hk_BasePrice.userName == UserAPI.UserName).ListFirst<hk_BasePrice>();
                        }
                        else
                        {
                            listp.Process = TaskAPI.Process;
                            listp.Incident = TaskAPI.Incident;
                            listp.UserName = UserAPI.UserName;
                            listp.UserFullName = UserAPI.UserFullName;
                            listp.ApplicationNo = ApplicationNo.Text;
                            listp.Time = System.DateTime.Now;
                            listp.Status = 0;
                            listp.Department = UserAPI.Department;
                        }

                        listp.VD_Category = VD_Category.SelectedValue;
                        listp.DrpPriority = DrpPriority.SelectedValue;
                        //hk_BasePrice.Actions = VD_Category.SelectedItem.Text;
                        listp.Sorg_DC = Sorg_DC.Checked ? "1" : "0";
                        listp.Condition_Type = Condition_Type.Text;
                        listp.Sales_Organization = Sales_Organization.Text;
                        listp.Division = Division.SelectedValue;
                        listp.Distribution_Channel = Distribution_Channel.Text;
                        
                        
                      

                        
                        (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == 0 & t_Attachment.username == UserAPI.UserName).Edit<t_Attachment>(p =>
                        {
                            p.Incident = TaskAPI.Incident;
                        });

                        Storage.Save(Storage.Context1, listp);
                    }
                    #endregion

            Save();

                    #region Priority功能
            if (TaskAPI.IsBeginStep && !string.IsNullOrEmpty(DrpPriority.SelectedValue))
            {
                mdc_Priority pro;
                if ((mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).Count<mdc_Priority>() > 0)
                {
                    pro = new mdc_Priority();
                    pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                    pro.Business_Priority = Convert.ToInt32(DrpPriority.SelectedValue);
                    Storage.Save(pro);
                }
                else
                {
                    pro = new mdc_Priority();
                    pro.Process = TaskAPI.Process;
                    pro.Incident = TaskAPI.Incident;
                    pro.Business_Priority = Convert.ToInt32(DrpPriority.SelectedValue);
                    Storage.Save(pro);
                }

            }


            if ((TaskAPI.Step == "DMO Maintain" || TaskAPI.Step == "DMO Approve" || TaskAPI.Step == "DMO Check") && !string.IsNullOrEmpty(this.DMO_DrpPriority.SelectedValue) && !string.IsNullOrEmpty(DrpPriority.SelectedValue))
            {

                //mdc_Priority pro;
                //pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                //pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                //Storage.Save(pro);
                mdc_Priority pro;
                if ((mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).Count<mdc_Priority>() > 0)
                {
                    pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                    pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                    Storage.Save(pro);
                }
                else
                {
                    pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                    pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                    Storage.Save(pro);
                }


            }
            #endregion

                   #region “DMO Maintain”步骤
            if (TaskAPI.Step == "DMO Maintain" || TaskAPI.Step == "DMO Approve" || TaskAPI.Step == "DMO Check")
            {

                TaskAPI["DMO_DrpPriority"] = DMO_DrpPriority.SelectedItem.Text;
                (hk_BasePrice.process == TaskAPI.Process & hk_BasePrice.incident == TaskAPI.Incident).Edit<hk_BasePrice>(p =>
                {
                    // p.SAP_Code = SAP_Code.Text;
                    p.DMO_DrpPriority = DMO_DrpPriority.SelectedValue;
                    p.DMO_Arrpvoe_Creation_Number = DMO_Arrpvoe_Creation_Number.Text;
                    p.DMO_Arrpvoe_Creation_Time = DMO_Arrpvoe_Creation_Time.Text;
                    p.DMO_Maintain_Creation_Number = DMO_Maintain_Creation_Number.Text;
                    p.DMO_Maintain_Creation_Time = DMO_Maintain_Creation_Time.Text;
                    p.DMO_Check_Creation_Number = DMO_Check_Creation_Number.Text;
                    p.DMO_Check_Creation_Time = DMO_Check_Creation_Time.Text;
                    p.DMO_Arrpvoe_Revision_Number = DMO_Arrpvoe_Revision_Number.Text;
                    p.DMO_Arrpvoe_Revision_Time = DMO_Arrpvoe_Revision_Time.Text;
                    p.DMO_Maintain_Revision_Number = DMO_Maintain_Revision_Number.Text;
                    p.DMO_Maintain_Revision_Time = DMO_Maintain_Revision_Time.Text;
                    p.DMO_Check_Revision_Number = DMO_Check_Revision_Number.Text;
                    p.DMO_Check_Revision_Time = DMO_Check_Revision_Time.Text;

                    //p.DMO_SalesSAP_Code = DMO_SalesSAP_Code.Text;
                }
                );
            }
            #endregion

                    Storage.Save(Assessment1.Assessment);    //审批意见

                    //多节点退回
                    if (!string.IsNullOrEmpty(Assessment1.JumpStep))
                    {
                        Assessment1.SetAsReturn(true);
                        TaskAPI.DirectActive(Assessment1.JumpStep);
                    }
                    else
                    {
                        Assessment1.SetAsReturn(false);
                    }
                    Assessment1.SubmitTo();

                    flg = TaskAPI.Send();
                });

                //如果数据库写入成功且流程发起成功的情况，将业务表数据与流程实例号进行关联
                if (TaskAPI.IsInitiate)
                {
                    if (!string.IsNullOrEmpty(DrpPriority.SelectedValue))
                    {
                        (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == 0).Edit<mdc_Priority>(p =>
                        {
                            p.Incident = TaskAPI.Incident;
                        });
                    }
                    
                    (hk_BasePrice.process == TaskAPI.Process & hk_BasePrice.incident == 0 & hk_BasePrice.userName == UserAPI.UserName).Edit<hk_BasePrice>(p => 
                    {
                        p.Incident = TaskAPI.Incident;
                    });

                    (t_Assessment.process == TaskAPI.Process & t_Assessment.incident == 0 & t_Assessment.userName == UserAPI.UserName).Edit<t_Assessment>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });

                    (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == 0 & t_Attachment.username== UserAPI.UserName).Edit<t_Attachment>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });

                    
                    (hk_BasePrice_Gridview.process == TaskAPI.Process & hk_BasePrice_Gridview.incident == 0 & hk_BasePrice_Gridview.userName == UserAPI.UserName).Edit<hk_BasePrice_Gridview>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });

                    
                }
                #endregion

                Send.Visible = false;
                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('" + Resources.Global.C_00_Success + "',true)");
            }
            catch (Exception ex)
            {
                TaskDB.Log("", false, "调用NVA方法Send_Click出错：" + ex.ToString());
                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('" + Resources.Global.C_00_Failed + "',false)");
            }
        }
        #endregion

       

        #region 事件 Category变换
        protected void VD_Category_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetEnable((VD_Category.SelectedValue == "1" || VD_Category.SelectedValue == "2" || VD_Category.SelectedValue == "3" || VD_Category.SelectedValue == "4"), DrpPriority,Division, gv_Base_Price);
            
        }
        #endregion

       

        #region 方法 InitDropdownList
        private void InitDropDownList()
        {
            if (!IsLoaded)
            {
                
                IsLoaded = true;
            }
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            //if (!Status.IsEdited)
            //{
            //    return;
            //}

            foreach (hk_BasePrice_Gridview BPM_1 in (List<hk_BasePrice_Gridview>)gv_Base_Price.DataSource)
            {
                if (string.IsNullOrEmpty(BPM_1.Material) && string.IsNullOrEmpty(BPM_1.Description) && string.IsNullOrEmpty(BPM_1.Unit) && string.IsNullOrEmpty(BPM_1.Calculat_Type) && string.IsNullOrEmpty(BPM_1.ScaleBasis))
                {
                    continue;
                }
                BPM_1.Process = TaskAPI.Process;
                BPM_1.Incident = TaskAPI.Incident;
                BPM_1.UserName = UserAPI.UserName;
                BPM_1.Time = System.DateTime.Now;
                BPM_1.Status = 0;
                Storage.Save(BPM_1);
            }
          
        }
           
          #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            List<hk_BasePrice_Gridview> base_1_s = (List<hk_BasePrice_Gridview>)gv_Base_Price.DataSource;
            hk_BasePrice_Gridview base_1 = new hk_BasePrice_Gridview();
            base_1.No = base_1_s.Count + 1;
            //base_1_s.Insert(gv_Base_Price.ChangedRowIndex == -1 || gv_Base_Price.ChangedRowIndex >= base_1_s.Count ? base_1_s.Count + 1 : gv_Base_Price.ChangedRowIndex+1, base_1);
            base_1_s.Insert(base_1_s.Count, base_1);
            gv_Base_Price.DataSource = base_1_s;
            gv_Base_Price.DataBind();
        }
        #endregion
        //#region 事件 Delete(删除行)
        //protected void Delete_Click(object sender, EventArgs e)
        //{
        //    List<hk_BasePrice_Gridview> base_1_s = (List<hk_BasePrice_Gridview>)gv_Base_Price.DataSource;
        //    if (base_1_s.Count == 1)
        //    {
        //        return;
        //    }
        //    base_1_s.RemoveAt(gv_Base_Price.ChangedRowIndex == -1 || gv_Base_Price.ChangedRowIndex >= base_1_s.Count ? base_1_s.Count - 1 : gv_Base_Price.ChangedRowIndex);
        //    gv_Base_Price.DataSource = base_1_s;
        //    gv_Base_Price.DataBind();
        //}
        //#endregion

        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            List<hk_BasePrice_Gridview> base_1_s = (List<hk_BasePrice_Gridview>)gv_Base_Price.DataSource;
            IList<hk_BasePrice_Gridview> base1 = (hk_BasePrice_Gridview.process == TaskAPI.Process & hk_BasePrice_Gridview.incident == TaskAPI.Incident).List<hk_BasePrice_Gridview>();
            if (base_1_s.Count == 1)
            {

                return;
            }
            base_1_s.RemoveAt(gv_Base_Price.ChangedRowIndex == -1 || gv_Base_Price.ChangedRowIndex >= base_1_s.Count ? base_1_s.Count - 1 : gv_Base_Price.ChangedRowIndex);
            bool flag = false;
            foreach (hk_BasePrice_Gridview base_1 in base1)
            {

                foreach (hk_BasePrice_Gridview base_2 in base_1_s)
                {
                    if (base_2.ID == base_1.ID)
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    Storage.Delete(base_1);
                }
                flag = false;
            }
            gv_Base_Price.DataSource = base_1_s;
            gv_Base_Price.DataBind();
        }
        #endregion


        #region 方法 数据验证
        private List<string> DataValidation()
        {
            List<Regular> regs = new List<Regular>();
            regs.Add(new Regular(RegularExp.NotEmpty, Division.SelectedValue, "Divison 必填"));
            foreach (hk_BasePrice_Gridview BPM in (List<hk_BasePrice_Gridview>)gv_Base_Price.DataSource)
            {
                if (string.IsNullOrEmpty(BPM.Material) && string.IsNullOrEmpty(BPM.Amount) && string.IsNullOrEmpty(BPM.Valid_From) && string.IsNullOrEmpty(BPM.Valid_To))
                {
                    continue;
                }

                if (string.IsNullOrEmpty(BPM.Material))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, BPM.Material, "&nbsp;&nbsp;行" + BPM.No + ":Material 为必填 "));
                }


                if (string.IsNullOrEmpty(BPM.Amount))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, BPM.Amount, "&nbsp;&nbsp;行" + BPM.No + ":Amount 为必填 "));
                }


                if (string.IsNullOrEmpty(BPM.Valid_From))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, BPM.Valid_From, "&nbsp;&nbsp;行" + BPM.No + ":Valid From 为必填 "));
                }

                if (string.IsNullOrEmpty(BPM.Valid_To))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, BPM.Valid_To, "&nbsp;&nbsp;行" + BPM.No + ":Valid To 为必填 "));
                }

                //if (TaskAPI.Step == "DMO Approve" & Assessment1.Assessment.Opinion == "1")
                //{


                //    regs.Add(new Regular(RegularExp.NotEmpty, DMO_DrpPriority.SelectedValue, " Priority 必填"));


                //}
                
            //    if (TaskAPI.Step == "DMO Approve" & VD_Category.SelectedValue == "1" & Assessment1.Assessment.Opinion == "1")
            //    {

            //        if (DMO_Arrpvoe_Creation_Number.Text == "" | DMO_Arrpvoe_Creation_Time.Text == "")
            //        {
            //            regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Approve的Creation项必填"));
            //        }

            //    }
            //    if (TaskAPI.Step == "DMO Maintain" & VD_Category.SelectedValue == "1" & Assessment1.Assessment.Opinion == "1")
            //    {

            //        if (DMO_Maintain_Creation_Number.Text == "" | DMO_Maintain_Creation_Time.Text == "")
            //        {
            //            regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Maintain的Creation项必填"));
            //        }

            //    }
            //    if (TaskAPI.Step == "DMO Check" & VD_Category.SelectedValue == "1" & Assessment1.Assessment.Opinion == "1")
            //    {
            //        if (DMO_Check_Creation_Number.Text == "" | DMO_Check_Creation_Time.Text == "")
            //        {
            //            regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Check的Creation项必填"));
            //        }

            //    }
            //    if (TaskAPI.Step == "DMO Approve" & VD_Category.SelectedValue != "1" & Assessment1.Assessment.Opinion == "1")
            //    {
            //        if (DMO_Arrpvoe_Revision_Number.Text == "" | DMO_Arrpvoe_Revision_Time.Text == "")
            //        {
            //            regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Approve的Revision项必填"));
            //        }
            //        //if (SA2.Checked & VD_Category.SelectedValue == "1")
            //        //{
            //        //    regs.Add(new Regular(RegularExp.NotEmpty, DMO_SalesSAP_Code.Text, "Sales SAP Code 必填"));
            //        //}
            //    }
            //    if (TaskAPI.Step == "DMO Maintain" & VD_Category.SelectedValue != "1" & Assessment1.Assessment.Opinion == "1")
            //    {
            //        if (DMO_Maintain_Revision_Number.Text == "" | DMO_Maintain_Revision_Time.Text == "")
            //        {
            //            regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Maintain的Revision项必填"));
            //        }

            //    }
            //    if (TaskAPI.Step == "DMO Check" & VD_Category.SelectedValue != "1" & Assessment1.Assessment.Opinion == "1")
            //    {
            //        if (DMO_Check_Revision_Number.Text == "" | DMO_Check_Revision_Time.Text == "")
            //        {
            //            regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Check的Revision项必填"));
            //        }

            //    }
            }


            return Validation(regs);
        }
        #endregion

        #region 方法 Others
        protected void VD_Payment_Methods_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }
        private string getFormCheckList(Anthem .CheckBoxList ck)
        {
            string t = "";
            for (int i = 0; i < ck.Items.Count; i++)
                if (ck.Items[i].Selected)
                    t += "," + ck.Items[i].Value;
                else
                    t += ",";
            return t;
        }
        private Anthem.CheckBoxList setCheckList(string t, Anthem .CheckBoxList ck)
        {
            try
            {
                string[] tt = t.Split(',');

                for (int i = 0; i <= tt.GetUpperBound(0); i++)
                {
                    for (int j = 0; j < ck.Items.Count; j++)
                    {
                        if (ck.Items[j].Value == tt[i].ToString())
                        { ck.Items[j].Selected = true; break; }
                    }
                }
            }
            catch { }
            return ck;
        }
        #endregion

       
    }
}